package com.travel.server.manager.impl.command.customer.data;

import com.travel.common.excel.ExcelData;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.ExcelUtil;
import com.travel.server.manager.dto.request.PageRequest;
import com.travel.server.manager.dto.request.customer.data.ExportProjectCustomerAnalysisRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.dto.response.customer.data.QueryProjectCustomerAnalysisResponse.AnalysisItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/10 16:24
 * @Desc:
 */
@Component
@Slf4j
public class ExportProjectCustomerAnalysisCommand extends AbstractCommand<ExportProjectCustomerAnalysisRequest, EmptyResponse> {

    @Autowired
    private QueryProjectCustomerAnalysisCommand queryProjectCustomerAnalysisCommand;

    @Override
    protected void checkInput(ExportProjectCustomerAnalysisRequest request) {
        checkPageInput(request);
    }

    @Override
    public EmptyResponse handle(ExportProjectCustomerAnalysisRequest request) {
        List<AnalysisItem> list = queryProjectCustomerAnalysisCommand.makeList(new PageRequest());
        if(CollectionUtil.isEmpty(list)){
            throw new WorkException(WorkCode.EXCEL_DATA_NULL);
        }
        List<String> title = Arrays.asList("项目", "年龄段", "男性占比", "女性占比");
        List<List<Object>> rows = new ArrayList<>();
        for(AnalysisItem item : list){
            List<Object> objects = new ArrayList<>();
            objects.add(item.getProject());
            objects.add(item.getAge());
            objects.add(item.getMan());
            objects.add(item.getWoman());
            rows.add(objects);
        }
        ExcelData excelData = new ExcelData();
        excelData.setTitles(title);
        excelData.setRows(rows);
        excelData.setName("项目客户分析");

        ExcelUtil.exportExcel(getHttpServletResponse(), "项目客户分析", excelData);

        return new EmptyResponse();
    }
}
